package com.wink_172.activity;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.VideoView;

import com.alibaba.fastjson.JSON;
import com.wink_172.commons.R;
import com.wink_172.library.model.json.JsonTest;

import org.xutils.x;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestJavaActivity extends com.wink_172.library.activity.SmartActivity {

    private ImageButton ib_main_imagebutton;
    private MediaPlayer mMediaPlayer;
    private SeekBar sb_main_bar;
    private boolean isPlay=false;
    private MyThread myThread;
    SurfaceView surfaceView;
    private SurfaceHolder holder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_java);

        initView();
        init();

//        String test = "[58][15][26][14][47]3[49][30][1][73][74]6[77][73][61][4][55][62][90]0[31][83][74]6[38][38][38]119";
//        getTargetStr(test);


        //Java获取个十百位数
        int bbbb=1234;
        Log.e(TAG, "test: ====>>"+bbbb%10 );//获取个位
        Log.e(TAG, "test: ====>>"+(bbbb/10)%10  );//获取十位
        Log.e(TAG, "test: ====>>"+(bbbb/100)%10 );//获取百位
        Log.e(TAG, "test: ====>>"+(bbbb/1000)%10 );//获取千位
         Log.e(TAG, "test: ====>>"+(bbbb/10000)%10 );//获取万位
        Log.e(TAG, "test: ====>>"+(bbbb/100000)%10 );//获取十万位
        "test: ====>>".replaceAll("","");
        int core=100>0?30:50;

        surfaceView=findViewById(R.id.sv_sureface);
        ib_main_imagebutton = (ImageButton) findViewById(R.id.ib_main_imagebutton);
        sb_main_bar = (SeekBar) findViewById(R.id.sb_main_bar);

        startPlayVideo();


String[] ss="ss".split("-");
        String[] ss2=new String[2];

        List<String> list=new ArrayList<>();

     }

     private String[] testbb(){
        return null;
     }



    private void startPlayVideo() {

        // 创建一个MediaPlayer对象
        mMediaPlayer = new MediaPlayer();
        // 设置播放的视频数据源
        try {
            mMediaPlayer.setDataSource("https://static.hongbaodada.com/o_1f9tro3vl1ug91na914991dba1u0r9.mp4");
        } catch (IOException e) {
//            Toast.makeText(this, "视频地址错误", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

        // 播放准备，使用异步方式，配合OnPreparedListener
        mMediaPlayer.prepareAsync();
        // 设置相关的监听器
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                isPlay=false;
                myThread=null;
                ib_main_imagebutton.setImageResource(android.R.drawable.ic_media_play);
                sb_main_bar.setProgress(0);
            }
        });
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mMediaPlayer.start();
                //2.改变图标(暂停)
                ib_main_imagebutton.setImageResource(android.R.drawable.ic_media_pause);
                //4.获取到音乐播放总时长
                int lengthOfTime=mMediaPlayer.getDuration();

                Log.e(TAG, "onPrepared: ====>>lengthOfTime:"+lengthOfTime );
                sb_main_bar.setMax(lengthOfTime);
                //6.开启线程
                isPlay=true;
                myThread = new MyThread();
                myThread.start();
            }
        });
        mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(MediaPlayer mediaPlayer, int videoX, int videoY) {
            }
        });
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {

                return false;
            }
        });
        mMediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {

                return false;
            }
        });
        //播放时屏幕保持唤醒
        mMediaPlayer.setScreenOnWhilePlaying(true);

        //显示图像
        holder = surfaceView.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                mMediaPlayer.setDisplay(holder);

            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            }
        });

        //给进度条设置监听
        sb_main_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //获取SeekBar拖动之后的进度
                int progress=seekBar.getProgress();
                //跳转到某个位置播放
                mMediaPlayer.seekTo(progress);

            }
        });




    }



    private static final String TAG = "TestActivity";
    //5.线程
    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            while(sb_main_bar.getProgress()<=sb_main_bar.getMax()){
                //得到音乐当前播放的位置
                int currentPosition=mMediaPlayer.getCurrentPosition();
                Log.e(TAG, "run: ====>>currentPosition:"+currentPosition );
                //设置进度条的进度
                sb_main_bar.setProgress(currentPosition);
                //一秒走一次
                SystemClock.sleep(50);

                if (!isPlay){
                    break;
                }
            }
        }
    }

    /**
     * 得到json文件中的内容
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line.trim());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


    private void initView() {

    }

    private void init() {
    }
}